package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.ArticlePageDTO;
import com.sky.dto.ArticleStatus;
import com.sky.dto.ArticleUpdateDTO;
import com.sky.entity.Article;
import com.sky.enumeration.OperationType;
import com.sky.vo.ArticleUpdateVO;
import com.sky.vo.ArticleVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

@Mapper
public interface ArticleMapper {
    /*
     * 新增文章
     * @param article
     */
    @AutoFill(value = OperationType.INSERT)
    @Insert("insert into article " +
            "(title, content, category_id, view_count, like_count, comment_count, is_top, is_recommend, user_id, create_time, update_time)" +
            "values " +
            "(#{title}, #{content}, #{categoryId}, #{viewCount}, #{likeCount}, #{commentCount}, #{isTop}, #{isRecommend}, #{userId}, #{createTime}, #{updateTime}) ")
   //获取文章id
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertArticle(Article article);

    /**
     * 分页查询文章
     * @param articlePageDTO
     * @return
     */
    Page<ArticleVO> pageQuery(ArticlePageDTO articlePageDTO);

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @Delete("delete from article where id = #{id}")
    void deleteById(Long id);



    /**
     * 根据id修改文章
     * @param articleUpdateDTO
     * @return
     */
    @AutoFill(value = OperationType.UPDATE)
    void updateArticle(ArticleUpdateDTO articleUpdateDTO);

    /**
     * 根据id查询文章
     * @param id
     * @return
     */

    @Select("select * from article where id = #{id}")
    Article getById(Long id);


    /**
     * 统计文章数量
     * @return
     */
    @Select("select count(*) from article")
    Long countArticle();

    /**
     * 根据分类id查询改分类下所有的文章
     * @param categoryId
     * @return
     */
    @Select("select * from article where category_id = #{categoryId}")
    List<ArticleVO> list(Long categoryId);

    /**
     * 修改文章点赞
     * @param article
     */
    @Update("update article set like_count = #{likeCount} where id = #{id}")
    void updateArticleLike(Article article);

    /**
     * 根据文章id列表查询文章
     * @param articleIds
     * @return
     */
    Page<ArticleVO> pageByArticleIds(List<Long> articleIds);

    List<String> findAllArticleByCategory(String category);

    @Select("select content from article where title = #{articleName}")
    String findArticleContent(String articleName);

    /**
     * 根据文章名称，查询该文章的标签
     */
    List<String> findArticleTag(String articleName);

    /**
     * 根据文章名称，查询该文章的分类
     */
    String findArticleCategory(String articleName);

    /**
     * 根据文章id，更新文章浏览量
     */
    @Update("update article set view_count = #{newCount} where id = #{id}")
    void updateArticleViewCount(long newCount, Long id);

    /**
     * 根据文章id列表查询文章
     * @param topIds
     * @return
     */
    @Select("select * from article where id in (#{topIds})")
    List<ArticleVO> listByIds(List<Long> topIds);

}
